//
//  FQDCommentMessageViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDCommentMessageViewController.h"
#import "FQDMySendOutCommentViewController.h"
#import "FQDCommentFromMeHeaderCell.h"
#import "FQDCommentOnMeCell.h"
#import "FQDQIconEmptyCell.h"
#import "FQDSimpleFeedCellItemProvider.h"
#import "FQDMyCommentMessageCellItem.h"
#import "QDailyAPI.h"
#import "FQDRestModel.h"

@interface FQDCommentMessageViewController ()

@end

@implementation FQDCommentMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCellItemProvider];
    [self loadNewCellItemsWithCompletion:nil];
}

- (void)setupCellItemProvider
{
    FQDSimpleFeedCellItemProvider *provider = [[FQDSimpleFeedCellItemProvider alloc] init];
    provider.responseFeedsName = @"comments";
    provider.responseFeedClass = [FQDMyCommentMessage class];
    provider.cellItemClass = [FQDMyCommentMessageCellItem class];
    [provider setFetchFeedBlock:^(NSString *feedId, NSString *cursorKey, QDailyAPICompleteBlock completion) {
        [[QDailyAPI sharedAPI] getMyCommentMessages:cursorKey completion:completion];
    }];
    self.provider = provider;
}

#pragma mark - FQDFeedBaseViewController DataSource
- (NSArray *)cellClasses
{
    return @[
        [FQDCommentFromMeHeaderCell class],
        [FQDQIconEmptyCell class],
        [FQDCommentOnMeCell class],
    ];
}

- (NSInteger)numberOfHeaderCellsInCollectionView:(UICollectionView *)collectionView
{
    BOOL hasFeeds = [self.provider.cellItems count] > 0;
    BOOL hasMoreCellItems = [self.provider hasMoreCellItems];
    BOOL shouldShowEmptyCell = (!hasFeeds && !hasMoreCellItems);
    NSInteger headerCount = (shouldShowEmptyCell) ? 2 : 1;
    return headerCount;
}

- (Class)headerCellClassAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    if (indexPath.section == 0) {
        return [FQDCommentFromMeHeaderCell class];
    } else if (indexPath.section == 1) {
        return [FQDQIconEmptyCell class];
    }
    return nil;
}

- (Class)cellClassForCellItem:(FQDFeedCellItem *)cellItem
{
    return [FQDCommentOnMeCell class];
}

#pragma mark - FQDFeedBaseViewController Deleagte
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetsForHeaderCellSection:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 5, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForHeaderCellAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cvSize = collectionView.frame.size;
    CGFloat topHeaderCellHeight = 54.0f;
    if (indexPath.section == 0) {
        return CGSizeMake(cvSize.width, topHeaderCellHeight);
    } else if (indexPath.section == 1) {
        UIEdgeInsets insets = [self collectionView:collectionView layout:collectionViewLayout insetsForHeaderCellSection:0];
        return CGSizeMake(cvSize.width, cvSize.height - insets.bottom - topHeaderCellHeight);
    }
    return CGSizeMake(cvSize.width, 100.0f);
}

- (void)configureHeaderCell:(UICollectionViewCell *)headerCell atIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    if (indexPath.section == 1) {
        FQDQIconEmptyCell *emptyCell = (FQDQIconEmptyCell *)headerCell;
        emptyCell.titleText = @"我收到的评论会出现在这里";
    }
}

- (void)didSelectHeaderCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    FQDMySendOutCommentViewController *commentVC = [[FQDMySendOutCommentViewController alloc] init];
    [self.navigationController pushViewController:commentVC animated:YES];
}

@end
